#include "W3oCon.h"

W3oCon::W3oCon()
{
	//ctor
}

W3oCon::~W3oCon()
{
	//dtor
}

void Modification::load(ifstream &ifs)
{
	int i;
	readIFS(ifs,i);
	modId=id2Str(i);
	readIFS(ifs,vType);
	if (1)
	{
		readIFS(ifs,level);
		readIFS(ifs,dptr);
	}
	switch (vType)
	{
	case 0:
		readIFS(ifs,mInt);
		break;
	case 1:
	case 2:
		readIFS(ifs,mReal);
		break;
	case 3:
		readIFS(ifs,mStr);
		break;
	}
	readIFS(ifs,endSign);
}
void Modification::save(ofstream &ofs)
{
	int i;
	i=str2Id(modId);
	writeOFS(ofs,i);
	writeOFS(ofs,vType);
	if (1)
	{
		writeOFS(ofs,level);
		writeOFS(ofs,dptr);
	}
	switch (vType)
	{
	case 0:
		writeOFS(ofs,mInt);
		break;
	case 1:
	case 2:
		writeOFS(ofs,mReal);
		break;
	case 3:
		writeOFS(ofs,mStr);
		break;
	}
	writeOFS(ofs,endSign);
}

void ObjectDefine::load(ifstream &ifs)
{
	int i;
	readIFS(ifs,i);
	ooid=id2Str(i);
	readIFS(ifs,i);
	noid=id2Str(i);
	readIFS(ifs,modAmount);
	mods.resize(modAmount);
	for (i=0;i<modAmount;i++)
	{
		Modification mod;
		mod.load(ifs);
		mods[i]=mod;
	}
}
void ObjectDefine::save(ofstream &ofs)
{

	int i;
	vector<Modification>::iterator iter;
	i=str2Id(ooid);
	writeOFS(ofs,i);
	i=str2Id(noid);
	writeOFS(ofs,i);
	modAmount=mods.size();
	writeOFS(ofs,modAmount);
	for (iter=mods.begin();iter!=mods.end();iter++)
		(*iter).save(ofs);
}

void ObjectTable::load(ifstream &ifs)
{
	int i;
	readIFS(ifs,objAmount);
	for (i=0;i<objAmount;i++)
	{
		ObjectDefine od;
		od.load(ifs);
		string id,ooid=od.ooid,noid=od.noid;
		if (noid=="") id=ooid;
		else id=noid;
		objDefs[id]=od;
	}
}
void ObjectTable::load(ifstream &ifs,char t)
{
	int i,j;
	readIFS(ifs,objAmount);
	for (i=0;i<objAmount;i++)
	{
		int tmp;
		ObjectDefine od;
		readIFS(ifs,tmp);
		od.ooid=id2Str(tmp);
		readIFS(ifs,tmp);
		od.noid=id2Str(tmp);
		readIFS(ifs,od.modAmount);
		od.mods.resize(od.modAmount);
		for (j=0;j<od.modAmount;j++)
		{
			Modification mod;
			readIFS(ifs,tmp);
			mod.modId=id2Str(tmp);
			readIFS(ifs,mod.vType);
			if (t=='a' || t=='d' || t=='q')
			{
				readIFS(ifs,mod.level);
				readIFS(ifs,mod.dptr);
			}
			switch (mod.vType)
			{
			case 0:
				readIFS(ifs,mod.mInt);
				break;
			case 1:
			case 2:
				readIFS(ifs,mod.mReal);
				break;
			case 3:
				readIFS(ifs,mod.mStr);
				break;
			}
			readIFS(ifs,mod.endSign);
			od.mods[j]=mod;
		}
		string id,ooid=od.ooid,noid=od.noid;
		if (noid=="") id=ooid;
		else id=noid;
		objDefs[id]=od;
	}
}
void ObjectTable::save(ofstream &ofs)
{
	objAmount=objDefs.size();
	writeOFS(ofs,objAmount);
	map<string,ObjectDefine>::iterator iter;
	for (iter=objDefs.begin();iter!=objDefs.end();iter++)
		(*iter).second.save(ofs);
}
void ObjectTable::save(ofstream &ofs,char t)
{
	int tmp;
	objAmount=objDefs.size();
	writeOFS(ofs,objAmount);
	map<string,ObjectDefine>::iterator iter;
	for (iter=objDefs.begin();iter!=objDefs.end();iter++)
	{
		ObjectDefine od=(*iter).second;
		vector<Modification>::iterator viter;
		tmp=str2Id(od.ooid);
		writeOFS(ofs,tmp);
		tmp=str2Id(od.noid);
		writeOFS(ofs,tmp);
		od.modAmount=od.mods.size();
		writeOFS(ofs,od.modAmount);
		for (viter=od.mods.begin();viter!=od.mods.end();viter++)
		{
			Modification mod=(*viter);
			tmp=str2Id(mod.modId);
			writeOFS(ofs,tmp);
			writeOFS(ofs,mod.vType);
			if (t=='a' || t=='d' ||t=='q')
			{
				writeOFS(ofs,mod.level);
				writeOFS(ofs,mod.dptr);
			}
			switch (mod.vType)
			{
			case 0:
				writeOFS(ofs,mod.mInt);
				break;
			case 1:
			case 2:
				writeOFS(ofs,mod.mReal);
				break;
			case 3:
				writeOFS(ofs,mod.mStr);
				break;
			}
			writeOFS(ofs,mod.endSign);
		}
	}
}
void ObjectTable::scanMods(vector<ModInfo> &modinfos)
{
	ModInfo mi;
	objAmount=objDefs.size();
	map<string,ObjectDefine>::iterator iter;
	for (iter=objDefs.begin();iter!=objDefs.end();iter++)
	{
		ObjectDefine od=(*iter).second;
		vector<Modification>::iterator viter;
		od.modAmount=od.mods.size();
		for (viter=od.mods.begin();viter!=od.mods.end();viter++)
		{
			if (od.noid=="") mi.oid=od.ooid;
			else mi.oid=od.noid;
			mi.mod=(*viter);
			modinfos.push_back(mi);
		}
	}
}

int W3oCon::loadW3oFileOld(const char *path)
{
	ifstream ifs(path,ios::binary|ios::in);
	if (ifs.good()==false) return 1;
	readIFS(ifs,version);
	oot.load(ifs);
	cot.load(ifs);
	return 0;
}
void W3oCon::saveW3oFileOld(const char *path)
{
	ofstream ofs(path,ios::binary|ios::out);
	writeOFS(ofs,version);
	oot.save(ofs);
	cot.save(ofs);
}
int W3oCon::loadW3oFile(const char *path)
{
	ifstream ifs(path,ios::binary|ios::in);
	if (ifs.good()==false) return 1;
	int len=strlen(path);
	char t=path[len-1];
	objType=t;
	readIFS(ifs,version);
	oot.load(ifs,t);
	cot.load(ifs,t);
	ifs.close();
	return 0;
}
void W3oCon::saveW3oFile(const char *path)
{
	ofstream ofs(path,ios::binary|ios::out);
	writeOFS(ofs,version);
	oot.save(ofs,objType);
	cot.save(ofs,objType);
}

void W3oCon::scanAllMods(vector<ModInfo> &modinfos)
{
	oot.scanMods(modinfos);
	cot.scanMods(modinfos);
}


